package com.chencs.filter;

import com.alibaba.fastjson.JSON;
import com.chencs.utils.MD5Tools;
import com.chencs.vo.net.response.ResponseBase;
import netscape.javascript.JSObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import sun.security.provider.MD5;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Created by Administrator on 2017/10/10.
 */
public class MyInterceptor implements HandlerInterceptor {
    String key = "5bba5498436660a90656c35b2966b86d";

    @Override
    public boolean preHandle(HttpServletRequest req, HttpServletResponse httpServletResponse, Object o) throws Exception {
        boolean flag =true;
        long beginTime = System.currentTimeMillis();
        String token = req.getHeader("token");
        String timestamp = req.getHeader("timestamp");
        if (req.getServletPath().startsWith("/app")){
            if(token.equals("dadafang")){
                return true;
            }

            try {
             if((beginTime - Long.parseLong(timestamp))<=60000){
                 if(MD5Tools.MD5(timestamp+key).equals(token)){
                //     System.out.println(">>>密码正确");
                     flag = true;
                 }else{
                  //   System.out.println(">>>非法请求");
                     flag = false;
                     ResponseBase response = new ResponseBase();
                     response.setCode(-1000);
                 //    response.setMsg("非法请求");
                     String json = JSON.toJSONString(response);
                     this.returnJson(httpServletResponse,json);
                 }
             }else{
              //   System.out.println(">>>请求超时");
                 flag = false;
                 ResponseBase response = new ResponseBase();
                 response.setCode(-1000);
             ///    response.setMsg("非法请求");
                 String json = JSON.toJSONString(response);
                 this.returnJson(httpServletResponse,json);
             }
         }catch (Exception e){
           //  System.out.println(">>>参数不完整");
             flag = false;
             ResponseBase response = new ResponseBase();
             response.setCode(-1000);
           //  response.setMsg("非法请求");
             String json = JSON.toJSONString(response);
             this.returnJson(httpServletResponse,json);
         }
        }else{
           // System.out.println(">>>不拦截");
            flag = true;
        }

        return flag;// 只有返回true才会继续向下执行，返回false取消当前请求

    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o,
                           ModelAndView modelAndView) throws Exception {
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object object, Exception exception) throws Exception{

    }

    private void returnJson(HttpServletResponse response, String json) throws Exception{
        PrintWriter writer = null;
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html; charset=utf-8");
        try {
            writer = response.getWriter();
            writer.print(json);

        } catch (IOException e) {

        } finally {
            if (writer != null)
                writer.close();
        }
    }


}